package com.tczs.server.response;

import com.tczs.server.util.FileUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**
 * 接口返回的对象数据
 */
public class Response {

    public static final String CSS = "text/css;charset=UTF-8";
    public static final String HTML = "text/html;charset=UTF-8";
    public static final String JSON = "application/json";
    public static final String STREAM = "application/octet-stream";

    /**
     * 返回 JSONObject
     * @param content
     * @return
     */
    public static FullHttpResponse okResponse(String content){
        return getResponse(content.getBytes(),HttpResponseStatus.OK,JSON);
    }

    /**
     * 返回自定义文件
     * @return
     */
    public static FullHttpResponse okStaticFileResponse(String filePath){
        FullHttpResponse fileResponse = getFileResponse(filePath, HttpResponseStatus.OK, HTML);
        fileResponse.headers().set("Cache-Control","max-age=300");
        return fileResponse;
    }

    /**
     * 返回静态文件
     * @return
     */
    public static FullHttpResponse okFileResponse(String filePath){
        return getFileResponse(filePath,HttpResponseStatus.OK,HTML);
    }

    /**
     * 返回静态CSS文件
     * @return
     */
    public static FullHttpResponse okCSSResponse(String filePath){
        return getFileResponse(filePath,HttpResponseStatus.OK,CSS);
    }

    /**
     * 返回字节流数据
     * @param filePath
     * @return
     */
    public static FullHttpResponse getStreamResponse(String filePath){
        return getFileResponse(filePath,HttpResponseStatus.OK,STREAM);
    }

    public static FullHttpResponse notFoundResponse(){
        return getFileResponse("./404.html",HttpResponseStatus.NOT_FOUND,HTML);
    }

    public static FullHttpResponse errorResponse(){
       return getFileResponse("./50x.html",HttpResponseStatus.INTERNAL_SERVER_ERROR,HTML);
    }

    private static FullHttpResponse getFileResponse(String filePath,HttpResponseStatus status,String contentType){
        byte[] result = FileUtil.toByteArray(filePath);
        if(filePath.endsWith(".css"))
            contentType = CSS;
        return getResponse(result,status,contentType);
    }

    public static FullHttpResponse getResponse(byte[] content,HttpResponseStatus status,String contentType){
        ByteBuf bu = Unpooled.copiedBuffer(content);
        FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                status,
                bu);
        fullHttpResponse.headers().set("Access-Control-Allow-Origin", "*");
     //   fullHttpResponse.headers().set("Access-Control-Allow-Headers", "Content-Type,Access-Token");
       // fullHttpResponse.headers().set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE,OPTION");  //允许访问的方式
        fullHttpResponse.headers().set("Content-Type",contentType);
        fullHttpResponse.headers().set("Content-Length", bu.writerIndex());
        return fullHttpResponse;
    }
}
